public class BinaryTree {
    /*二叉树的遍历
    前序遍历 ：先根遍历  根-》左子树 ——》右子树
        每次便利的时候每颗都是全新的树

    中序遍历  ：先遍历左子树 ——》 在遍历根 ——》在遍历右子树
    后续遍历 ： 先遍历左子树 ——》右子树 ——》根
    层序遍历 ：
     */
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //自动地创建一个树
    public static int i = 0;

    public static TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.left = H;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }

    //后续遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");

    }

    //求节点个数
    public int countNode = 0;
    public void nodeSize(TreeNode root) {
        if (root == null) {
            return;
        }
        countNode++;
        nodeSize(root.left);
        nodeSize(root.right);
    }

    //求节点个数
    //要求整棵树有多少个节点 实际上 = 左树的节点 + 右树的节点 + 1;
    public int nodeSize2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int temp = nodeSize2(root.left) + nodeSize2(root.right) +1;
        return temp;
    }

    //求叶子节点的个数
    //左树的叶子 + 右树的叶子 = 整棵树的叶子节点

    public int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        } else if (root.left == null && root.right == null) {
            return 1;
        } else {
            return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
        }
    }
    public int leafNodeSize = 0;
    public void getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return;
        } else if (root.left == null && root.right == null) {
            leafNodeSize++;
        } else {
            getLeafNodeCount2(root.left);
            getLeafNodeCount2(root.right);
        }
    }

    //第K层节点的个数
    public int getKLeveNodeSize(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLeveNodeSize(root.left,k-1) + getKLeveNodeSize(root.left,k-1);
    }

    /*
     * 求二叉树的高度
     * 时间复杂度：O(n)
     * 空间复杂度：O(logN)
     * @param root
     * @return
     */
    public int getHight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return getHight(root.left) > getHight(root.right) ?
                getHight(root.left) + 1 : getHight(root.right) + 1;
    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }
    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return Math.max(leftH,rightH) + 1;
    }

    //相同的树 力扣100
    //结构相同 + 数据值相同
    //结构相同 ： 要么同时为空 ， 要么同时不为空
    //根节点一样 && 左子节点一样 && 右子节点一样


    /*另一颗树的子树 力扣572

     */


    /*翻转二叉树 力扣226
        把每个左右子节点进行交换

     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root == null && root.right == null) {
            return null;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    /*判断一颗二叉树是否是平衡二叉树 力扣 110


     */
}
